<template>
	<view :style="colorStyle">
		<view class='searchGood'>
			<view class='search acea-row row-between-wrapper'>
				<view class='input acea-row row-between-wrapper'>
					<text class='iconfont icon-sousuo'></text>
					<input type='text' v-model='searchValue' @confirm="inputConfirm" confirm-type="search" focus placeholder='点击搜索商品'
					 placeholder-class='placeholder' @input="setValue"></input>
				</view>
				<view class='bnt' @tap='searchBut'>搜索</view>
			</view>
			<template v-if="history.length">
				<view class='title acea-row row-between-wrapper'>
					<view>搜索历史</view>
					<view class="iconfont icon-shanchu" @click="clear"></view>
				</view>
				<view class='list acea-row'>
					<block v-for="(item,index) in history" :key="index">
						<view class='item history-item line1' @tap='setHotSearchValue(item.keyword)' v-if="item.keyword">{{item.keyword}}</view>
					</block>
				</view>
			</template>
			<view class='title'>热门搜索</view>
			<view class='list acea-row'>
				<block v-for="(item,index) in hotSearchList" :key="index">
					<view class='item line1' @tap='setHotSearchValue(item.val)' v-if="item.val">{{item.val}}</view>
				</block>
			</view>
			
			
			
			<view class='line' v-if='bastList.length'></view>
			
			<view class='shupingShowBox' v-if="bastList.length > 0"
				v-for="(item,index) in bastList" :key="index">
			
				<view class="sepLineView"></view>
				<image mode="aspectFit" :src='item.image'  @click="godDetail(item)" class="hengpingimage"></image>
				
				
				<view class="titleshubang">{{item.store_name}}</view>
				
				
				<text class="activeShubangbiaoqian"
					v-if="item.activity && item.activity.type === '1' && !promotions_type">秒杀</text>
				<text class="activeShubangbiaoqian"
					v-if="item.activity && item.activity.type === '2' && !promotions_type">砍价</text>
				<text class="activeShubangbiaoqian"
					v-if="item.activity && item.activity.type === '3' && !promotions_type">拼团</text>
				<text class="activeShubangbiaoqian" v-if="item.promotions.title">{{item.promotions.title}}</text>
						
				
						
				<view class="PriceShuban" v-if="isStore==0">￥{{item.price}}</view>
				
				<view class="PriceShuban" v-if="isStore==1">￥{{item.vip_price}}</view>
						
				<image v-if="item.stock>0" @click="addTocar(item)" class="shubanaddcar" src='../../../static/images/addcar.png'></image>
				
				<view v-if="item.stock<=0" class="shubanNoStock" >已售罄</view>
				
			
			</view>
			
			
			
			
			<!-- {{bastList}} -->
			
	<!-- 		<view class='line' v-if='bastList.length'></view>
			<goodList :bastList="bastList" v-if="bastList.length > 0"></goodList>
 -->


<!-- 			

<view class='loadingicon acea-row row-center-wrapper' v-if="bastList.length > 0">
				<text class='loading iconfont icon-jiazai' :hidden='loading==false'></text>{{loadTitle}}
			</view> -->
			
			
			
		</view>
		<view class='noCommodity'>
			<view class='pictrue' v-if="bastList.length == 0">
				<!-- <image src='../../static/images/noSearch.png'></image> -->
				<image :src="imgHost + '/statics/images/noSearch.png'"></image>
			</view>
			<!-- <recommend :hostProduct='hostProduct' v-if="bastList.length == 0 && page > 1"></recommend> -->
		</view>
		<home v-if="navigation"></home>
	</view>
</template>

<script>
	import home from '@/components/home/index.vue'
	import {
		getSearchKeyword,
		getProductslist,
		getProductHot
	} from '@/api/store.js';
	import {
		postCartAdd
	} from '@/api/store.js';
	import {
		searchList,
		clearSearch
	} from '@/api/api.js';
	import {
		mapGetters
	} from "vuex";
	import {
		getUserInfo
	} from '@/api/user.js';
	import goodList from '@/components/goodList';
	import recommend from '@/components/recommend';
	import colors from "@/mixins/color";
	import {HTTP_REQUEST_URL} from '@/config/app.js';
	export default {
		computed: mapGetters(['uid', 'isLogin']),
		components: {
			goodList,
			recommend,
			home
		},
		mixins: [colors],
		data() {
			return {
				hostProduct: [],
				searchValue: '',
				focus: true,
				bastList: [],
				hotSearchList: [],
				first: 0,
				limit: 8,
				page: 1,
				loading: false,
				loadend: false,
				loadTitle: '加载更多',
				hotPage: 1,
				isScroll: true,
				store_id : 0,
				history: [],
				imgHost:HTTP_REQUEST_URL,
				isStore : 0,
			};
		},
		onLoad: function(options) {
			
			this.store_id = options.id ;
			
			console.log(this.store_id);
			
		},
		onShow: function() {
			
			if (this.isLogin) {
				
				this.getUserInfo();
			}
			
			uni.removeStorageSync('form_type_cart');
			// this.getRoutineHotSearch();
			this.getHostProduct();
			this.searchList();
			try {
				this.hotSearchList = uni.getStorageSync('hotList');
			} catch (err) {}
		},
		onReachBottom: function() {
			if (this.bastList.length > 0) {
				this.getProductList();
			} else {
				this.getHostProduct();
			}

		},
		methods: {
			
			/*
			 * 获取用户信息
			 */
			getUserInfo: function() {
				let that = this;
				getUserInfo().then(res => {
			
					if ( res.data.user_type == 'store' ){
			
						that.isStore = 1;
						
					}else{
						
						that.isStore = 0;
					
					}
					
					
					
					
				});
			},
			
			// 去详情页
			godDetail(item) {
				
				uni.navigateTo({
					url: `/pages/goods_details/index?id=${item.id}`
				})
			},
			
			
			addTocar(item){
				
				
				//是否登录
				if (this.isLogin === false) {
					
					return this.$util.Tips({
						title: '去登录'
					});
					
				} else {
					
					this.addToCarPost(item);
					
				}
				
				
				
				
			},
			
			
			addToCarPost(item){
				
				
				
				var that = this;
				
				
				var cartNum = 1;
				
				console.log(item);
				
				if (this.isStore==1 && item.minimum_store_inventory > 0){
					cartNum = item.minimum_store_inventory;
				}
				
				
				let q = {
					cartNum: cartNum,
					productId:item.id
				};
					
					
				
				postCartAdd(q)
					.then(function(res) {
						
						
						that.$util.Tips({
							title: '添加购物车成功',
							success: () => {
								that.cartNum = res.data.cartNum;
								that.getCartCount(true);
							}
						});
						
						
					})
					.catch(err => {
						that.isOpen = false;
						return that.$util.Tips({
							title: err
						});
					});
				
				
				
				
				
			},
			
			
			
			
			searchList() {
				searchList({
					page: 1,
					limit: 10
				}).then(res => {
					this.history = res.data;
				});
			},
			clear() {
				let that = this;
				clearSearch().then(res => {
					uni.showToast({
						title: res.msg,
						success() {
							that.history = [];
						}
					});
				});
			},
			inputConfirm: function(event) {
				if (event.detail.value) {
					uni.hideKeyboard();
					this.setHotSearchValue(event.detail.value);
				}
			},
			getRoutineHotSearch: function() {
				let that = this;
				getSearchKeyword().then(res => {
					that.$set(that, 'hotSearchList', res.data);
				});
			},
			getProductList: function() {
				let that = this;
				if (that.loadend) return;
				if (that.loading) return;
				that.loading = true;
				that.loadTitle = '';
				getProductslist({
					keyword: that.searchValue,
					page: that.page,
					limit: that.limit,
					store_id : that.store_id
				}).then(res => {
					let list = res.data,
						loadend = list.length < that.limit;
					that.bastList = that.$util.SplitArray(list, that.bastList);
					that.$set(that, 'bastList', that.bastList);
					that.loading = false;
					that.loadend = loadend;
					that.loadTitle = loadend ? "没有更多内容啦~" : "加载更多";
					that.page = that.page + 1;
				}).catch(err => {
					that.loading = false,
						that.loadTitle = '加载更多'
				});
			},
			getHostProduct: function() {
				let that = this;
				if (!this.isScroll) return
				getProductHot(that.hotPage, that.limit).then(res => {
					that.isScroll = res.data.length >= that.limit
					that.hostProduct = that.hostProduct.concat(res.data)
					that.hotPage += 1;
				});
			},
			setHotSearchValue: function(event) {
				this.$set(this, 'searchValue', event);
				this.page = 1;
				this.loadend = false;
				this.$set(this, 'bastList', []);
				this.getProductList();
			},
			setValue: function(event) {
				this.$set(this, 'searchValue', event.detail.value);
			},
			searchBut: function() {
				let that = this;
				that.focus = false;
				if (that.searchValue.length > 0) {
					that.page = 1;
					that.loadend = false;
					that.$set(that, 'bastList', []);
					uni.showLoading({
						title: '正在搜索中'
					});
					that.getProductList();
					uni.hideLoading();
				} else {
					return this.$util.Tips({
						title: '请输入要搜索的商品',
						icon: 'none',
						duration: 1000,
						mask: true,
					});
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #fff !important;
	}
	
	.noCommodity{
		border-top-width: 0;
	}

	.searchGood .search {
		padding-left: 30rpx;
	}

	.searchGood .search {
		margin-top: 20rpx;
	}

	.searchGood .search .input {
		width: 598rpx;
		background-color: #f7f7f7;
		border-radius: 33rpx;
		padding: 0 35rpx;
		box-sizing: border-box;
		height: 66rpx;
	}

	.searchGood .search .input input {
		width: 472rpx;
		font-size: 28rpx;
	}

	.searchGood .search .input .placeholder {
		color: #999;
	}

	.searchGood .search .input .iconfont {
		color: #555;
		font-size: 35rpx;
	}

	.searchGood .search .bnt {
		width: 120rpx;
		text-align: center;
		height: 66rpx;
		line-height: 66rpx;
		font-size: 30rpx;
		color: #282828;
	}

	.searchGood .title {
		font-size: 28rpx;
		color: #999;
		margin: 50rpx 30rpx 25rpx 30rpx;
	}

	.searchGood .title .iconfont {
		font-size: 28rpx;
	}

	.searchGood .list {
		padding-left: 10rpx;
	}

	.searchGood .list .item {
		font-size: 26rpx;
		color: #454545;
		padding: 0 21rpx;
		height: 60rpx;
		border-radius: 3rpx;
		line-height: 60rpx;
		border: 1rpx solid #aaa;
		margin: 0 0 20rpx 20rpx;
	}

	.searchGood .list .item.history-item {
		height: 50rpx;
		border: none;
		border-radius: 25rpx;
		background-color: #F7F7F7;
		line-height: 50rpx;
	}

	.searchGood .line {
		border-bottom: 1rpx solid #eee;
		margin: 20rpx 30rpx 0 30rpx;
	}
	
	
.shupingShowBox{
	
	
	// background-color: #65bc53 !important;
	position: relative;
	
	width: 100%;
	
	height: 209rpx;
	
	// margin-top: 20rpx;
	
	margin-left: 0rpx !important;
	
	background-color: white;
	
	
}
	
	
	


.hengpingimage{
	
	position: absolute;
	
	top: 26rpx;
	
	left : 26rpx;
	
	width: 197rpx;
	
	height: 164rpx;
	
}


.sepLineView{
	
	
	position: absolute;
	
	bottom: 0rpx;
	
	left : 240rpx;
	
	width: 510rpx;
	
	height: 1rpx;
	
	background-color: #E0E4ED;
	
}


.titleshubang{
	
	position: absolute;
	
	top: 26rpx;
	
	left : 241rpx;
	
	width: 480rpx;
	
	height: 28rpx;
	
	font-size: 26rpx;
	
	color: #222222;
	
	font-weight: bold;
	
	
}


.activeShubangbiaoqian{
	
	
	position: absolute;
	
	top: 76rpx;
	
	left : 241rpx;
	

	
	height: 32rpx;
	
	font-size: 20rpx;
	
	border: #B88F45 1px solid;
	
	color: #B88F45;
	
	border-radius: 6rpx;
	
	padding-left: 11rpx;
	
	padding-right: 11rpx;
	
	// font-weight: bold;
	
	
	
}


.PriceShuban{
	
	
	position: absolute;
	
	top: 152rpx;
	
	left : 239rpx;
	
	
	
	height: 32rpx;
	
	font-size: 36rpx;

	
	color: #E93D34;
	

	
	font-weight: bold;
	
	
	
	
}

.shubanaddcar{
	
	position: absolute;
	
	bottom: 19rpx;
	
	right : 30rpx;
	
	height: 46rpx;
	
	width: 46rpx;
	
}


.shubanNoStock{
	
	position: absolute;
	
	bottom: 19rpx;
	
	right : 30rpx;
	
	// height: 46rpx;
	
	color: #cccccc;
	
	font-size : 25rpx;
	
	// width: 46rpx;
	
}


.hengpingBox{
	
	position: relative;
	
	display: flex;
	
	flex-direction: row;
	
	flex-wrap: wrap;
	
	
	
	
}


.hengpingLeftBox{
	
	width: 344rpx;
	
	// height : 518rpx;
	
	background-color: white;
	
	border-radius: 20rpx;
	
	position: relative;
	
	margin-top: 24rpx;
	
	margin-left: 24rpx;
	
	display: flex;
	
	flex-direction: column;
	
	// align-items: ;
	
	
}

.shupingimage{
	
	margin-top: 24rpx;

	width: 344rpx;
		
	height: 344rpx;
	
}



.titlehengban{
	
	// position: absolute;
	
	margin-left: 18rpx;
	
	margin-top: 18rpx;
	
	width: 290rpx;
	

	
	font-size: 28rpx;
	
	color: #333333;
	
	font-weight: bold;
	
	// background-color: #65bc53;
	
	
}


.activeNameBox{
	
	position: relative;
	
	margin-top: 23rpx;
	
	margin-left: 21rpx;
	
	
	
	height: 32rpx;
	

	
	
}


.activehengbangbiaoqian{
	
	position: absolute;
	
	left: 0;
	
	top: 0;

	
	height: 32rpx;
	
	font-size: 20rpx;
	
	border: #B88F45 1px solid;
	
	color: #B88F45;
	
	border-radius: 6rpx;
	
	padding-left: 11rpx;
	
	padding-right: 11rpx;
	
	// font-weight: bold;
	
	
	
}




.PriceHengban{
	
	
	// position: absolute;
	
	// top: 152rpx;
	
	// left : 239rpx;
	
	position: relative;
	
	margin-top: 23rpx;
	
	margin-left: 21rpx;
	
	height: 32rpx;
	
	font-size: 36rpx;

	
	color: #E93D34;
	

	
	font-weight: bold;
	
	margin-bottom: 38rpx;
	
	
}	
	

</style>
